<template>
  <div>
    <el-table
      :data="tableData"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border
      :tree-props="{children: 'children'}"
    >
      <el-table-column prop="id" label="规格编号" sortable width="180"></el-table-column>
      <el-table-column prop="specsname" label="规格名称" width="180"></el-table-column>
      <el-table-column label="规格属性" width="180">
        <template v-slot="scope">
            <el-tag type="primary" v-for="item in scope.row.attrs" :key='item'>{{item}}</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="状态" width="180">
        <template v-slot="scope">
          <el-button v-if="scope.row.status === 1" type="success">启用</el-button>
          <el-button v-else type="danger">禁用</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" sortable width="180">
        <template v-slot="scope">
          <el-button type="success" @click="edit(scope.row.id)">编辑</el-button>
          <el-button type="danger" @click="del(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @current-change="cPage"
      :page-size="size"
      background
      layout="prev, pager, next"
      :total="total"
    ></el-pagination>
  </div>
</template>

<script>
import { mapActions, mapGetters } from "vuex";
import {delSpecs} from '../../../request/api'
import {successAlert} from '../../../utils/alert'
export default {
  computed: {
    ...mapGetters({
      "tableData": "specs/tableData",
      "size":"specs/size",
      "total":"specs/total"
    })
  },
  methods: {
    ...mapActions({
      totalSpeccs: "specs/totalActions",
      "requestList": "specs/specsListActions",
      "pageNow":'specs/pageAcitons',
      "oneSpecs":"specs/oneActions"
    }),
    del(id){
        this.$confirm('确定要删除吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
                }).then(() => {
                    // 发起删除请求
                    delSpecs({id}).then(res=>{
                        if(res.code === 200){
                            // 消息提示
                            this.totalSpeccs()
                            this.requestList()
                            successAlert(res.msg);
                        }
                    })
                }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });          
                });
    },
    cPage(page){
        this.pageNow(page)
        this.totalSpeccs()
        this.requestList();
    },
    edit(id){
        this.$emit('edit',id)
        
    }
  },
  mounted() {
    this.totalSpeccs(), this.requestList();
  }
};
</script>

<style>
</style>